//======================================
// C_Elements.c - Created by Xycaleth
//======================================
#include "../cgame/cg_local.h"

void C_RegisterElementMedia ( void )
{
	// Divine buster
	cgs.effects.ndaDivineBusterCircle	= trap_FX_RegisterEffect ("Element_MCp/MCp_Circle");
	cgs.effects.ndaDivineBusterBall		= trap_FX_RegisterEffect ("Element_MCp/MCp_BeamBall");
	cgs.effects.ndaDivineBusterBeam		= trap_FX_RegisterEffect ("Element_MCp/MCp_BeamBeam");
	cgs.effects.ndaDivineBusterRing		= trap_FX_RegisterEffect ("Element_MCp/MCp_BeamRing");
	cgs.media.ndaDivineBusterSound		= trap_S_RegisterSound ("sound/Element_MCp/MCp_DivineBuster.mp3");
}

void C_DoElements ( centity_t * cent )
{
	entityState_t	*s = &cent->currentState;

	if ( s->eFlags & EF_DEAD )
	{
		// Dead guy
		return;
	}

	if ( s->ndaFlags & (1 << F_CASTING) )
	{
		vec3_t	dir, pos, angles;

		if ( cent->elementInfo.thinkTime < cg.time )
		{
			// 50 degrees every 1 second...
			cent->elementInfo.generic1 = (cent->elementInfo.generic1 + 1) % 360;
			angles[YAW] = cent->elementInfo.generic1;

			cent->elementInfo.thinkTime = cg.time + 200;

			VectorCopy (cent->lerpAngles, angles);
			angles[ROLL] = angles[PITCH] = 0;
			AngleVectors (angles, NULL, NULL, dir);

			VectorCopy (cent->lerpOrigin, pos);
			pos[2] -= 22;

			trap_FX_PlayEffectID (cgs.effects.ndaDivineBusterCircle, pos, dir, -1, -1);
		}

		switch ( s->ndaElement )
		{
		case ELEMENT_ATTACK_BEAM:
			if ( cent->elementInfo.startTime == 0 )
			{
				cent->elementInfo.startTime = cg.time;
				cent->elementInfo.endTime = cg.time + ELEMENT_BEAM_DURATION + ELEMENT_BEAM_CHARGE;
				cent->elementInfo.chargeTime = cg.time + ELEMENT_BEAM_CHARGE;
			}

			if ( cent->elementInfo.endTime <= cg.time )
			{
				cent->elementInfo.thinkTime = 0;
				return;
			}
			else
			{
				vec3_t	start, fwd, axis[3];

				VectorCopy (cent->lerpOrigin, start);
				start[2] += DEFAULT_VIEWHEIGHT;

				AngleVectors (cent->lerpAngles, fwd, NULL, NULL);
				AnglesToAxis (cent->lerpAngles, axis);

				VectorMA (start, 30, fwd, start);
				trap_FX_PlayEntityEffectID (cgs.effects.ndaDivineBusterBall, start, axis, -1, -1, -1, -1);
				trap_FX_PlayEntityEffectID (cgs.effects.ndaDivineBusterRing, start, axis, -1, -1, -1, -1);

				if ( cent->elementInfo.chargeTime > cg.time )
				{
					if ( !cent->elementInfo.soundPlayed )
					{
						trap_S_StartSound (cent->lerpOrigin, cent->currentState.number, CHAN_BODY, cgs.media.ndaDivineBusterSound);
						cent->elementInfo.soundPlayed = qtrue;
					}
				}
				else
				{
					trap_FX_PlayEntityEffectID (cgs.effects.ndaDivineBusterBeam, start, axis, -1, -1, -1, -1);
				}
			}
			
			break;

		default:
			break;
		}
	}
	else
	{
		memset (&cent->elementInfo, 0, sizeof (cent->elementInfo));
	}
}

// eof
